import { useEffect, useState } from "react";

const useCountriesData = ()=>{
  const [countries, setCountries] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(false);
  const fetchData = async () => {
    try {
      setError(false);
      setLoading(true);
      let response = await fetch("https://restcountries.com/v2/all");
      let countries = await response.json();
      countries = countries.filter((elem) => elem.name !== "Taiwan");
      setCountries(countries);
      setLoading(false);
    } catch (err) {
      console.log(err);
      setLoading(false);
      setError(true);
    }
  };
  useEffect(() => {
    fetchData();
  }, []);
  return { countries,setCountries,error,setError,loading,setLoading, fetchData }
}

export default useCountriesData;